package com.lh.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.lh.pojo.User;
import com.lh.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lh
 * @since 2021-02-26
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户")
public class UserController {

    @Autowired
    private UserService userServiceImpl;

    /**
     * 新增用户
     */
    @PostMapping("/insertUser")
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @DynamicParameters(properties = {
            @DynamicParameter(name = "id", value = "ID", example = "XXXXXX", required = true, dataTypeClass = String.class),
            @DynamicParameter(name = "username", value = "用户名", required = true),
            @DynamicParameter(name = "password", value = "密码", required = true),
            @DynamicParameter(name = "sex", value = "性别"),
            @DynamicParameter(name = "locked", value = "是否锁定"),
    })
//    @ApiOperationSupport(ignoreParameters = {"createBy", "createTime", "updateBy", "updateTime"})
    public String insertUser(@RequestBody JSONObject user) {
        boolean isAdd = userServiceImpl.save(user.toJavaObject(User.class));
        if (isAdd) {
            return "新增成功";
        } else {
            return "新增失败";
        }
    }

    /**
     * 根据id查询用户
     */
    @GetMapping("/getById")
    @ApiOperation(value = "根据id查询用户", notes = "根据id查询用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true)
//    @ApiImplicitParams({})
//    @ApiOperationSupport(ignoreParameters = {"id", "name"})
//    @ApiOperationSupport(order = 40, includeParameters = {"id,name"})
    public User findUserById(@RequestParam("id") String id) {
        User user = userServiceImpl.getById(id);
        if (user != null) {
            return user;
        } else {
            System.out.println("null");
            return null;
        }
    }

}

